﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.User
{
    public partial class Add : Base.BasePage
    {
        ExtendBLL.UserTable EBut = new ExtendBLL.UserTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("YHGL");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                tb.Style.Add("display","none");
                tbRealName.Attributes.Add("readOnly","true");
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbUserName.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "用户名不能为空！");
                tbUserName.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(tbRealName.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "真实姓名不能为空！");
                tbRealName.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(tbPassword.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "密码不能为空！");
                tbPassword.Focus();
                return;
            }
            else
            {
                if (!EBut.IsExistUserName(tbUserName.Text.Trim()))//用户名已经存在，不能注册
                {
                    Common.JShelper.JSAlert(this.Page, "", "用户名已经存在！");
                    tbUserName.Text = "";
                    tbUserName.Focus();
                    return;
                }
                else
                {
                    //往用户表里添加信息
                    LabMS.Model.UserTable Mut = new LabMS.Model.UserTable();
                    DataSet ds = new DataSet();
                    Mut.CreateTime = DateTime.Now;

                    Mut.UserName = tbUserName.Text.Trim();
                    Mut.Password = tbPassword.Text.Trim();
                    Mut.PCode = tb.Text.Trim();
                    ds = EBut.GetTeacherByPCode(tb.Text.Trim());
                    //Mut.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                    //Mut.Tel = ds.Tables[0].Rows[0]["Tel"].ToString();
                    if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["Staff_Birth"].ToString()))
                    {
                        DateTime d1 = DateTime.Parse(ds.Tables[0].Rows[0]["Staff_Birth"].ToString());
                        DateTime d2 = DateTime.Now;
                        Mut.Age = d2.Year - d1.Year;
                    }

                    Mut.SexCode = ds.Tables[0].Rows[0]["Staff_Sex"].ToString();

                    LabMS.BLL.UserTable But = new LabMS.BLL.UserTable();
                    hiddenAddUserID.Value=But.Add(Mut).ToString();
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('新增用户成功！');window.location.href='UserDefault.aspx';</script>");
                    AddRole.Visible = true;//显示“添加角色按钮”
                    PanRole.Visible = true;
                }
            }
        }


        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AddRole_Click(object sender, EventArgs e)
        {
            //根据新增的用户信息查找UserID
            int UserID =int.Parse(hiddenAddUserID.Value.ToString());
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"","<script type='text/javascript'>window.open('AddRole.aspx?UserID="+UserID+"','','top=200,left=250,height=300,width=400');</script>");
        }

        protected void gvRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }
    }
}
